﻿#include "form3.h"
#include "ui_form3.h"

#include "cairo.h"
#include <QDebug>
#include <QPainter>

Form3::Form3(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::Form3)
{
    ui->setupUi(this);
}

Form3::~Form3()
{
    delete ui;
}

//https://www.cairographics.org/samples/
void Form3::paintEvent(QPaintEvent *pEvent)
{
    Q_UNUSED(pEvent);

    //创建一张图QImage
    int iW = this->width();
    int iH = this->height();
    QImage image;
    image.load("d:\\clr.jpg"); //clr
    unsigned char *pData = image.bits();
    int iLineStride = image.bytesPerLine();

    //建立
    cairo_surface_t *surface = cairo_image_surface_create_for_data(pData, CAIRO_FORMAT_ARGB32, iW, iH, iLineStride);
    cairo_surface_set_device_scale(surface, image.devicePixelRatio(), image.devicePixelRatio());
    cairo_t *cr = cairo_create(surface);

    //写文本
    cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size(cr, 90.0);
    cairo_set_source_rgb(cr, 1, 0, 0);

    cairo_move_to(cr, 10.0, 150.0);
    cairo_show_text(cr, "Hello");

    //销毁
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    //贴图
    QPainter painter(this);
    painter.drawImage(this->rect(), image);
}
